<?php


declare(strict_types=1);

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use App\Utils\ApiUtils;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
// mongodb
use Hyperf\Mongodb\MongoDb;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Parallel;//引入@Inject注解

/**
 * @Controller()
 */
class MongodbController extends AbstractController
{

    /**
     * @Inject()
     * @var MongoDb
     */
    protected $mongoDbClient;

    private $db_name = 'pc_wxuser';
    /**
     * mongodb 自动创建table数据
     * Abell 2020/5/19
     * @RequestMapping(path="/abell/mongodb/install", methods="get")
     */
    public function install(){
        //创建小程序用户表  pc_wxuser
        $insert = [
            'openid' => 'ogtzs4bmnorPZFkdXiFgo0IjADX1',
            'nickname' => '呢称',
            "pid"=>"openid",
            "devicecode"=>"设备号",
            "avatarurl"=>"https://avatar.csdnimg.cn/2/1/A/3_gjalj10.jpg",
            "gender"=>1,
            "country"=> "中国",
            "province"=> "江苏省",
            "city"=> "无锡市",
            "language"=> "zh",
            "add_time"=> 1589019140,
            "mobile"=> 15512345678,
            "update_time"=> 1589019140,
            "haspay"=> 1,
            "statue"=> 0,
            "hasadd"=> 1
        ];
        $user_result = $this->mongoDbClient->insert('pc_wxuser',$insert);
        // 创建 小程序活动通知表  pc_wxnotice
        $wxnotice_insert =[
            'title' => '活动标题',
            'content' => '活动内容',
            "img"=>"https://avatar.csdnimg.cn/2/1/A/3_gjalj10.jpg",
            "type"=>1, //活动类型1 外链 2 文本编辑器 3 无
            "is_del"=> 1, // 是否删除 默认1,删除2
            "start_time"=> 1589019140,
            "end_time"=> 1589019140,
            "add_time"=> 1589019140,
            "update_time"=> 1589019140,
        ];
        $wxnotice_result = $this->mongoDbClient->insert('pc_wxnotice',$wxnotice_insert);
        //  小程序活动通知类型  pc_wxnoticetype
        $wxnoticetype_insert = [
            'title' => '通知类型标题',
            "add_time"=> 1589019140,
            "update_time"=> 1589019140,
        ];
        $wxnoticetype_result = $this->mongoDbClient->insert('pc_wxnoticetype',$wxnoticetype_insert);
        //问题反馈表   pc_wxservice
        $wxservice_insert = [
            'openid' => 'ogtzs4bmnorPZFkdXiFgo0IjADX1',
            'title' => '问题反馈提交标题',
            "detail"=>"问题反馈提交详情",
            "mobile"=>"联系方式",
            'statue'=>0, // 用户状态 0 未处理 1 正在处理 2 已经处理 3 情况待定
            'reply' => '后台回复客户',
            'type'  => 1 //问题反馈类型
        ];
        $wxservice_result = $this->mongoDbClient->insert('pc_wxservice',$wxservice_insert);
        // 问题反馈类型表 pc_wxservicetype
        $wxservicetype_insert = [
            'title' => '问题反馈类型标题',
            "add_time"=> 1589019140,
            "update_time"=> 1589019140,
            'type'  => 1,
            'is_del'   => 1 // 是否删除 默认1,删除2
        ];
        $wxservicetype_result = $this->mongoDbClient->insert('pc_wxservicetype',$wxservicetype_insert);
        // 小程序用户签到表 pc_wxsign
        $wxsign_insert = [
            'openid' => 'ogtzs4bmnorPZFkdXiFgo0IjADX1',
            "add_time"=> 1589019140,
            "update_time"=> 1589019140
        ];
        $wxsign_result = $this->mongoDbClient->insert('pc_wxsign',$wxsign_insert);
        // 小程序访问记录 pc_wxvisit
        $wxvisit_insert = [
            'openid' => 'ogtzs4bmnorPZFkdXiFgo0IjADX1',
            'pid' => 'fadfad', // 是否是总统计id 0 基础统计 1 子页面统计
            "type_explain"=>"/home/hello", //访问页面路径
            "watch"=>1, //查看次数
            "add_time"=> 1589019140,
            "update_time"=> 1589019140
        ];
        $wxvisit_result = $this->mongoDbClient->insert('pc_wxvisit',$wxvisit_insert);
        // 微信后台访问记录  pc_wxdata
        $wxdata_insert = [
            'visit_total' => 11, // 累计用户数
            'share_pv' => 1,  // 转发次数
            'share_uv' => 1,  // 转发人数
            'daily_retain' => 'fasdfadsfa', // 日留存数据json
            "add_time"=> 1589019140
        ];
        $wxdata_result = $this->mongoDbClient->insert('pc_wxdata',$wxdata_insert);
        return $this->response->json(ApiUtils::send(0,'ok',[
            'pc_wxuser' => $user_result, 'pc_wxnotice' => $wxnotice_result ,'pc_wxnoticetype' => $wxnoticetype_result,
            'pc_wxservice' => $wxservice_result,'pc_wxservicetype' =>$wxservicetype_result,
            'pc_wxsign' => $wxsign_result,'pc_wxvisit'=>$wxvisit_result,'pc_wxdata'=>$wxdata_result
        ],1));
    }

    /**
     * mongodb 用户列表数据数量展示
     * Abell 2020/5/15
     * @RequestMapping(path="/abell/mongodb/list", methods="get")
     */
    public function list(RequestInterface $request){
//        $result = $this->mongoDbClient->fetchAll($this->db_name);
        $count = $this->mongoDbClient->count($this->db_name);
        var_dump($count);
        return $this->response->json(ApiUtils::send(0,'ok',$count,1));
    }

    /**
     * mongodb 插入单条数据
     * Abell 2020/5/15
     * @RequestMapping(path="/abell/mongodb/insert", methods="get")
     */
    public function insert(RequestInterface $request){
        // 问题反馈类型表 pc_wxservicetype
        $wxservicetype_insert = [
            'title' => '问题反馈类型标题',
            "add_time"=> 1589019140,
            "update_time"=> 1589019140,
            'type'  => 1,
            'is_del'   => 1 // 是否删除 默认1,删除2
        ];
        $wxservicetype_result = $this->mongoDbClient->insert('pc_wxservicetype',$wxservicetype_insert);
        return $this->response->json(ApiUtils::send(0,'ok',$wxservicetype_result,1));
    }

    /**
     * mongodb 插入多条数据
     * Abell 2020/5/15
     * @RequestMapping(path="/abell/mongodb/insert_many", methods="get")
     */
    public function insertMany(RequestInterface $request){
        $insert = [[
            'openid' => 'ogtzs4bmnorPZFkdXiFgo0IjADX2',
            'nickname' => '布莱恩',
            "pid"=>"ogtzs4bmnorPZFkdXiFgo0IjADX",
            "devicecode"=>"ogtzs4LbS6R8Y_ec6k64nUNWIeC",
            "avatarurl"=>"https://avatar.csdnimg.cn/2/1/A/3_gjalj10.jpg",
            "gender"=>1,
            "country"=> "中国",
            "province"=> "江西省",
            "city"=> "南昌市",
            "language"=> "zh",
            "add_time"=> 1589019140,
            "mobile"=> 15512345678,
            "update_time"=> 1589019140,
            "haspay"=> 1,
            "statue"=> 0,
            "hasadd"=> 1
        ],[
            'openid' => 'ogtzs4bmnorPZFkdXiFgo0IjADX3',
            'nickname' => '陆瓷',
            "pid"=>"ogtzs4bmnorPZFkdXiFgo0IjADX",
            "devicecode"=>"ogtzs4LbS6R8Y_ec6k64nUNWIeC",
            "avatarurl"=>"https://avatar.csdnimg.cn/2/1/A/3_gjalj10.jpg",
            "gender"=>1,
            "country"=> "中国",
            "province"=> "河南省",
            "city"=> "开封市",
            "language"=> "zh",
            "add_time"=> 1589019140,
            "mobile"=> 15512345678,
            "update_time"=> 1589019140,
            "haspay"=> 1,
            "statue"=> 0,
            "hasadd"=> 1
        ]];
        $result = $this->mongoDbClient->insertAll($this->db_name,$insert);
        return $result;
    }

    /**
     * hello 协程随机插入用户数据
     * Abell 2020/5/15
     * @RequestMapping(path="/abell/mongodb/hello", methods="get")
     */
    public function hello(){
        $aepara = new Parallel();
        $start_time = date( "Y-m-d H:i",time());
        $aepara->add(function (){
            for ($i = 0; $i<90000;$i++){
                $this->randWxuser();
            }
        });
        $aepara->add(function (){
            for ($i = 0; $i<90000;$i++){
                $this->randWxuser();
            }
        });
        $aepara->add(function (){
            for ($i = 0; $i<90000;$i++){
                $this->randWxuser();
            }
        });
        var_dump('运行结束,开始时间');
        var_dump($start_time);
        var_dump('结束时间');
        var_dump(date( "Y-m-d H:i",time()));
        $result = $aepara->wait();
        return $result;
    }

    private function open_id($head = "ogtzs4", $length = 21, $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-')
    {
        if (!is_int($length) || $length < 0) {
            return '$length 必须为Int类型';
        }
        $string = '';
        for ($i = $length; $i > 0; $i--) {
            $string .= $char[mt_rand(0, strlen($char) - 1)];
        }
        return $head . $string;
    }

    private function randWxuser(){
        // 新建一个用户数据
        $insert = [
            'openid' => $this->open_id(),
            'nickname' => $this->randName(),
            "pid"=>"",
            "devicecode"=>"",
            "avatarurl"=>"https://avatar.csdnimg.cn/2/1/A/3_gjalj10.jpg",
            "gender"=>rand(0,1),
            "country"=> "中国",
            "province"=> "广东省",
            "city"=> $this->randAddress(),
            "language"=> "zh",
            "add_time"=> time(),
            "mobile"=> $this->randPhone(),
            "update_time"=> time(),
            "haspay"=> rand(0,1),
            "statue"=> 0,
            "hasadd"=> rand(0,1)
        ];
        $this->mongoDbClient->insert($this->db_name,$insert);
    }

    // 随机生成手机号
    private function randPhone($num=9){
        $code_array_num = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
        $random_phone = array();
        $head_phone = [13,14,15,16,17,18,19];

        for ($i = 0; $i < $num; $i++) {
            $key = array_rand($code_array_num, 1);
            array_push($random_phone, $code_array_num[$key]);
        }
        $verification_phone = implode('', $random_phone);
        return $head_phone[array_rand($random_phone)].$verification_phone;
    }
    // 随机生成姓名
    private function randName(){
        $first_name_arr = array('赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨',
            '朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹','喻',
            '柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方',
            '任','袁','柳','鲍','史','唐','费','薛','雷','贺','倪','汤','滕','殷','罗','毕','郝','安','常','傅',
            '卞','齐','元','顾','孟','平','黄','穆','萧','尹','姚','邵','湛','汪','祁','毛','狄','米','伏','成',
            '戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝','董','梁','杜','阮','蓝','闵','季','贾','路',
            '娄','江','童','颜','郭','梅','盛','林','钟','徐','邱','骆','高','夏','蔡','田','樊','胡','凌','霍',
            '虞','万','支','柯','管','卢','莫','柯','房','裘','缪','解','应','宗','丁','宣','邓','单','杭','洪',
            '包','诸','左','石','崔','吉','龚','程','嵇','邢','裴','陆','荣','翁','荀','于','惠','甄','曲','封',
            '储','仲','伊','宁','仇','甘','武','符','刘','景','詹','龙','叶','幸','司','黎','溥','印','怀','蒲',
            '邰','从','索','赖','卓','屠','池','乔','胥','闻','莘','党','翟','谭','贡','劳','逄','姬','申','扶',
            '堵','冉','宰','雍','桑','寿','通','燕','浦','尚','农','温','别','庄','晏','柴','瞿','阎','连','习',
            '容','向','古','易','廖','庾','终','步','都','耿','满','弘','匡','国','文','寇','广','禄','阙','东',
            '欧','利','师','巩','聂','关','荆','司马','上官','欧阳','夏侯','诸葛','闻人','东方','赫连','皇甫',
            '尉迟','公羊','澹台','公冶','宗政','濮阳','淳于','单于','太叔','申屠','公孙','仲孙','轩辕','令狐',
            '徐离','宇文','长孙','慕容','司徒','司空');
        $last_name_arr = array('伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','明','永',
            '健','世','广','志','义','兴','良','海','山','仁','波','宁','贵','福','生','龙','元','全','国','胜',
            '学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星',
            '光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','诚','先','敬','震','振','壮',
            '会','思','群','豪','心','邦','承','乐','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲',
            '江','超','浩','亮','政','谦','亨','奇','固','之','轮','翰','朗','伯','宏','言','若','鸣','朋','斌',
            '梁','栋','维','启','克','伦','翔','旭','鹏','泽','晨','辰','士','以','建','家','致','树','炎','德',
            '行','时','泰','盛','雄','琛','钧','冠','策','腾','楠','榕','风','航','弘','秀','娟','英','华','慧',
            '巧','美','娜','静','淑','惠','珠','翠','雅','芝','玉','萍','红','娥','玲','芬','芳','燕','彩','春',
            '菊','兰','凤','洁','梅','琳','素','云','莲','真','环','雪','荣','爱','妹','霞','香','月','莺','媛',
            '艳','瑞','凡','佳','嘉','琼','勤','珍','贞','莉','桂','娣','叶','璧','璐','娅','琦','晶','妍','茜',
            '秋','珊','莎','锦','黛','青','倩','婷','姣','婉','娴','瑾','颖','露','瑶','怡','婵','雁','蓓','纨',
            '仪','荷','丹','蓉','眉','君','琴','蕊','薇','菁','梦','岚','苑','婕','馨','瑗','琰','韵','融','园',
            '艺','咏','卿','聪','澜','纯','毓','悦','昭','冰','爽','琬','茗','羽','希','欣','飘','育','滢','馥',
            '筠','柔','竹','霭','凝','晓','欢','霄','枫','芸','菲','寒','伊','亚','宜','可','姬','舒','影','荔',
            '枝','丽','阳','妮','宝','贝','初','程','梵','罡','恒','鸿','桦','骅','剑','娇','纪','宽','苛','灵',
            '玛','媚','琪','晴','容','睿','烁','堂','唯','威','韦','雯','苇','萱','阅','彦','宇','雨','洋','忠',
            '宗','曼','紫','逸','贤','蝶','菡','绿','蓝','儿','翠','烟');
        $first_name = $first_name_arr[array_rand($first_name_arr)];
        $last_name = $last_name_arr[array_rand($last_name_arr)];
        $last_name_double = array_rand($last_name_arr,2);
        $last_name_two = $last_name_arr[$last_name_double[0]].$last_name_arr[$last_name_double[1]];
        return rand(0,1) ? $first_name.$last_name : $first_name.$last_name_two;
    }

    //随机省市
    private function randAddress(){
        $cities = ['广州市','深圳市','珠海市','汕头市','韶关市','佛山市','江门市','湛江市','茂名市','肇庆市','惠州市',
            '梅州市','汕尾市','河源市','阳江市','清远市','潮州市','揭阳市','云浮市','东莞市','中山市'];
        return $cities[array_rand($cities)];
    }
}




